package cn.wolfcode.business.service.impl;

import cn.wolfcode.business.domain.BusAppointment;
import cn.wolfcode.business.domain.BusStatement;
import cn.wolfcode.business.mapper.BusinessStatementMapper;
import cn.wolfcode.business.service.AppointmentService;
import cn.wolfcode.business.service.BusinessStatementService;
import cn.wolfcode.business.util.DateUtil;
import cn.wolfcode.business.util.RegexUtils;
import cn.wolfcode.business.util.VehiclePlateNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class BusinessStatementServiceImpl implements BusinessStatementService {
    @Autowired
    private BusinessStatementMapper businessStatementMapper;
    @Autowired
    private AppointmentService appointmentService;

    @Override
    public List<BusStatement> selectStatementList(BusStatement busStatement) {
        Map<String, Object> map = busStatement.getParams();
        if (map!=null){
            String endActualArrivalTime = (String) map.get("endActualArrivalTime");
            if (endActualArrivalTime!=null){
                map.put("endActualArrivalTime",endActualArrivalTime+" 23:59:59");
            }
        }
        return businessStatementMapper.selectStatementList(busStatement);
    }

    @Override
    public BusStatement selectStatementById(Long id) {
        return businessStatementMapper.selectStatementById(id);
    }

    @Override
    public int save(BusStatement busStatement) {
        if (busStatement == null) {
            throw new RuntimeException("参数不能为空");
        }
        if (busStatement.getCustomerName() == null || busStatement.getCustomerPhone() == null ||
                busStatement.getActualArrivalTime() == null || busStatement.getLicensePlate() == null ||
                busStatement.getServiceType() == null || busStatement.getStatus() == null) {
            throw new RuntimeException("参数不能为空");
        }
        if (!RegexUtils.isPhoneLegal(busStatement.getCustomerPhone())) {
            throw new RuntimeException("手机号格式不正确");
        }
        if (VehiclePlateNoUtil.getVehiclePlateNo(busStatement.getLicensePlate()) == null) {
            throw new RuntimeException("车牌号格式不正确");
        }
        Date update = DateUtil.changeDate(new Date(), Calendar.MINUTE, 1);
        if (!busStatement.getActualArrivalTime().before(update)) {
            throw new RuntimeException("实际到店时间不能小于当前时间");
        }
        busStatement.setStatus(BusStatement.STATUS_CONSUME);
        busStatement.setIsDelete(BusStatement.IS_NOT_DELETE);
        busStatement.setCreateTime(new Date());
        return businessStatementMapper.insert(busStatement);
    }

    @Override
    public int update(BusStatement busStatement) {
        if (busStatement == null) {
            throw new RuntimeException("参数不能为空");
        }
        if (busStatement.getCustomerName() == null || busStatement.getCustomerPhone() == null ||
                busStatement.getActualArrivalTime() == null || busStatement.getLicensePlate() == null ||
                busStatement.getServiceType() == null || busStatement.getStatus() == null) {
            throw new RuntimeException("参数不能为空");
        }
        if (!RegexUtils.isPhoneLegal(busStatement.getCustomerPhone())) {
            throw new RuntimeException("手机号格式不正确");
        }
        if (VehiclePlateNoUtil.getVehiclePlateNo(busStatement.getLicensePlate()) == null) {
            throw new RuntimeException("车牌号格式不正确");
        }
        Date update = DateUtil.changeDate(new Date(), Calendar.MINUTE, 1);
        if (!busStatement.getActualArrivalTime().before(update)) {
            throw new RuntimeException("实际到店时间不能小于当前时间");
        }
        BusStatement old = this.selectStatementById(busStatement.getId());
        if (old == null || old.getStatus() == null || old.getIsDelete() == null) {
            throw new RuntimeException("参数不能为空");
        }
        if (!old.getStatus().equals(busStatement.getStatus()) || !old.getIsDelete().equals(busStatement.getIsDelete())) {
            throw new RuntimeException("参数不能修改");
        }
        if (BusStatement.IS_DELETE.equals(busStatement.getIsDelete())) {
            throw new RuntimeException("只有在未删除才可以进行编辑操作");
        }
        if(BusStatement.STATUS_PAID.equals(busStatement.getStatus())){
            throw new RuntimeException("只有在未付款才可以进行编辑操作");
        }
        return businessStatementMapper.update(busStatement);
    }

    @Override
    @Transactional
    public int deleteStatementById(Long id) {
        if (id == null) {
            throw new RuntimeException("参数不能为空");
        }
        BusStatement old = this.selectStatementById(id);
        if (old == null || old.getStatus() == null || old.getIsDelete() == null) {
            throw new RuntimeException("参数不能为空");
        }
        if (BusStatement.IS_DELETE.equals(old.getIsDelete())) {
            throw new RuntimeException("只有在未删除才可以进行删除操作");
        }
        if (BusStatement.STATUS_PAID.equals(old.getStatus())) {
            throw new RuntimeException("只有在未付款才可以进行删除操作");
        }
        if(old.getAppointmentId()!=null){
            BusAppointment busAppointment = appointmentService.selectAppointmentById(old.getAppointmentId());
            if(busAppointment==null){
                throw new RuntimeException("参数不能为空");
            }
            if (!BusAppointment.STATUS_SETTLED.equals(busAppointment.getStatus())) {
                throw new RuntimeException("只有在未结算才可以进行删除操作");
            }
            appointmentService.updateStatus(busAppointment.getId(), BusAppointment.STATUS_ARRIVED);
        }
        return businessStatementMapper.delete(id, BusStatement.IS_DELETE);
    }

    @Override
    public void updateAmount(Long statementId, BigDecimal totalAmount, BigDecimal totalQuantity, BigDecimal discountAmount) {
        businessStatementMapper.updateAmount(statementId,totalAmount,totalQuantity,discountAmount);
    }

    @Override
    public int updatePayStatus(Long statementId, Long payId, Date date, Integer statusPaid) {
        return businessStatementMapper.updatePayStatus(statementId,payId,date,statusPaid);
    }

    @Override
    @Transactional
    public Long createStatement(Long id) {
        if (id==null){
            throw new RuntimeException("参数不能为空");
        }
        BusAppointment busAppointment = appointmentService.selectAppointmentById(id);
        if (busAppointment==null){
            throw new RuntimeException("非法参数");
        }
        BusStatement busStatement =  businessStatementMapper.selectByAppointmentId(id);

        if (busStatement!=null){
            if (!(BusAppointment.STATUS_SETTLED.equals(busAppointment.getStatus())||
                BusAppointment.STATUS_PAID.equals(busAppointment.getStatus())||
                BusAppointment.STATUS_ARRIVED.equals(busAppointment.getStatus())&&
                BusStatement.IS_DELETE.equals(busStatement.getIsDelete()))){
                throw new RuntimeException("订单状态不正确");
            }
        }else {
            if (!BusAppointment.STATUS_ARRIVED.equals(busAppointment.getStatus())){
                throw new RuntimeException("订单状态不正确");
            }
            busStatement = new BusStatement();
            busStatement.setCustomerName(busAppointment.getCustomerName());
            busStatement.setCustomerPhone(busAppointment.getCustomerPhone());
            busStatement.setActualArrivalTime(busAppointment.getActualArrivalTime());
            busStatement.setLicensePlate(busAppointment.getLicensePlate());
            busStatement.setCarSeries(busAppointment.getCarSeries());
            busStatement.setServiceType(busAppointment.getServiceType());
            busStatement.setInfo(busAppointment.getInfo());
            busStatement.setAppointmentId(id);
            busStatement.setStatus(BusStatement.STATUS_CONSUME);
            busStatement.setDiscountAmount(new BigDecimal("0.0"));
            busStatement.setIsDelete(BusStatement.IS_NOT_DELETE);
            businessStatementMapper.insert(busStatement);
            appointmentService.updateStatus(id,BusAppointment.STATUS_SETTLED);
        }
        if (BusStatement.IS_DELETE.equals(busStatement.getIsDelete())){
            businessStatementMapper.delete(busStatement.getId(), BusStatement.IS_NOT_DELETE);
            appointmentService.updateStatus(id,BusAppointment.STATUS_SETTLED);
        }
        return busStatement.getId();
    }
}